﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.IO;

namespace workshop3
{
    public partial class Formaddrubric : Form
    {
        int percentage = 0;
        DataTable rubric = new DataTable();
        DataColumn itemname = new DataColumn("itemname");
        DataColumn dcpercentage = new DataColumn("percentage");

        List<string> yearlist = new List<string>();
        List<string> semelist = new List<string>();
         public Formaddrubric( List<string> yearlist, List<string> semelist)
        {
            InitializeComponent();
            this.yearlist = yearlist;
            this.semelist = semelist;
        }

        private void Formaddrubric_Load(object sender, EventArgs e)
        {
            rubric.Columns.Add(itemname);
            rubric.Columns.Add(dcpercentage);
            cbyear.SelectedValueChanged -= cbyear_SelectedValueChanged;
            cbsemester.SelectedValueChanged -= cbsemester_SelectedValueChanged;
            cbcourse.SelectedValueChanged -= cbcourse_SelectedValueChanged;
            //cbrubric.SelectedValueChanged -= cbrubric_SelectedValueChanged;

            cbyear.DataSource = yearlist;
            cbsemester.DataSource = semelist;

            cbyear.SelectedValueChanged += cbyear_SelectedValueChanged;
            cbsemester.SelectedValueChanged += cbsemester_SelectedValueChanged;
        }


        private void cbcourse_SelectedValueChanged(object sender, EventArgs e)
        {

        }

        private void ChangeCourse(string year, string semester)//this is to dynamic show the course in the combobox
        {
            DataTable dt0 = new DataTable();
            DataColumn dc3 = new DataColumn("coursename");
            DataColumn dc4 = new DataColumn("courseid");
            dt0.Columns.Add(dc4);
            dt0.Columns.Add(dc3);
            if (File.Exists("course.xml"))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("course.xml");
                string path = "//course";
                XmlNodeList nodelist = doc.SelectNodes(path);
                if (nodelist.Count != 0)
                {
                    for (int i = 0; i < nodelist.Count; i++)
                    {
                        if (nodelist[i].ChildNodes[1].InnerText == year && nodelist[i].ChildNodes[2].InnerText == semester)
                        {
                            dt0.Rows.Add(nodelist[i].Attributes[0].Value, nodelist[i].ChildNodes[0].InnerText);
                        }

                    }

                    cbcourse.DataSource = dt0;
                    cbcourse.DisplayMember = dt0.Columns[1].ToString();
                    cbcourse.ValueMember = dt0.Columns[0].ToString();
                    cbcourse.SelectedValueChanged += cbcourse_SelectedValueChanged;


                }
                else
                {
                    MessageBox.Show("Can not find any course!");
                }

                //return dt0;

            }
            //return null;
        }

        private void cbyear_SelectedValueChanged(object sender, EventArgs e)
        {
            ChangeCourse(cbyear.SelectedValue.ToString(), cbsemester.SelectedValue.ToString());
        }

        private void cbsemester_SelectedValueChanged(object sender, EventArgs e)
        {
            ChangeCourse(cbyear.SelectedValue.ToString(), cbsemester.SelectedValue.ToString());
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            if (!ClassCreateXML.TextCheck(txtitemname.Text))
            {
                return;
            }
            if (ClassCreateXML.TextCheck(txtpercentage.Text))
            {
                return;
            }
            if (txtitemname.Text != null && txtpercentage.Text != null)
            {
                int temp = 0;
                int.TryParse(txtpercentage.Text, out temp);
                percentage = percentage + temp;
                if (percentage <= 100)
                {
                    rubric.Rows.Add(txtitemname.Text, txtpercentage.Text);
                    dgvrubirc.DataSource = rubric;
                }
                else
                {
                    MessageBox.Show("The total percentage is overhead 100. Please input valid percentage!");
                    percentage = percentage - temp;
                }
                txtitemname.Text = string.Empty;
                txtpercentage.Text = string.Empty;
                lbltotal.Text = percentage.ToString();
            }
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            if (percentage != 100) {
                MessageBox.Show("wrong persentage");
                return ;
            }

            if (rubric.Rows.Count > 0)//check if the user has add any item in it
            {
                DialogResult dr = MessageBox.Show("Once you have been pressed the 'Confirm' button, you do not allow to change!", "Warning", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.OK)
                {
                    ClassCreateXML.AddRubricXml(rubric, txtrubricname.Text, cbcourse.SelectedValue.ToString());
                    this.Close();
                }
                if (dr == DialogResult.Cancel)
                {

                }
            }
            else
            {
                MessageBox.Show("You have not input any data.");
            }
        }

        private void dgvrubirc_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)//do the delete
            {
                percentage = 0;
                int temp;
                //int.TryParse(dtams.Rows[e.RowIndex][1].ToString(), out temp);
                //percentage = percentage - temp;
                rubric.Rows.Remove(rubric.Rows[e.RowIndex]);
                for (int i = 0; i < rubric.Rows.Count; i++)
                {
                    int.TryParse(rubric.Rows[i][1].ToString(), out temp);
                    percentage = percentage + temp;
                }
                lbltotal.Text = percentage.ToString();
            }
        }

        private void dgvrubirc_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            percentage = 0;
            int temp;
            //int.TryParse(dtams.Rows[e.RowIndex][1].ToString(), out temp);
            for (int i = 0; i < rubric.Rows.Count; i++)
            {
                int.TryParse(rubric.Rows[i][1].ToString(), out temp);
                percentage = percentage + temp;
            }
            lbltotal.Text = percentage.ToString();
        }

        private void cbcourse_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cbyear_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

